package com.ztgf.sys.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.sys.models.enums.SYSCodeEnum;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.sys.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.sys.mapper.SysPlatformMapper;
import com.ztgf.sys.models.service.ISysPlatformService;
import com.ztgf.sys.models.bo.SysPlatformSaveBO;
import com.ztgf.sys.models.dto.SysPlatformDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 平台表控制器
 *
 * @author mxli
 * @Date 2020-05-27 10:14:27
 */
@DubboService
public class SysPlatformServiceImpl extends ServiceImpl<SysPlatformMapper, SysPlatformDTO> implements ISysPlatformService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private SysPlatformMapper sysPlatformMapper;

    @Override
    public boolean add(SysPlatformSaveBO saveBO) {
        SysPlatformDTO dto = new SysPlatformDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 获取默认的平台模板
     * @return
     */
    @Override
    public SysPlatformDTO getDefaultPlatform(){
        Wrapper<SysPlatformDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_default", 0);
        List<SysPlatformDTO> sysPlatformDTOList = sysPlatformMapper.selectList(wrapper);
        if(sysPlatformDTOList.size() > 0){
            return sysPlatformDTOList.get(0);
        }
        return null;
    }

    /**
     * 模板删除后 删除对应的关系
     * @param temId
     * @return
     */
    @Override
    public boolean deleteTemRelation(Long temId){
        return sysPlatformMapper.deleteTemRelateionLogIc(temId);
    }

    /**
     * 逻辑删除 模板删除后 删除对应的关系
     * @param temId
     * @return
     */
    @Override
    public boolean deleteTemRelationLogic(Long temId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("home_templet_id", temId).or().eq("activity_templet_id", temId);
        return sysPlatformMapper.delete(wrapper) > 0;
    }
}
